﻿using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Microsoft.IdentityModel.Tokens;
using StaticFilesSample.Models;

namespace StaticFilesSample.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class AuthorizeController : ControllerBase
    {
        private readonly JwtSettings _jwtSettings;
        public AuthorizeController(IOptions<JwtSettings> optionAccessor)
        {
            _jwtSettings = optionAccessor.Value;
        }
        public IActionResult Token()
        {
            var claims = new List<Claim>();
            claims.Add(new Claim(ClaimTypes.Name, "gainorloss"));
            claims.Add(new Claim("password", "p@ssw0rd"));

            var cred = new SigningCredentials(new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSettings.SecretKey)), SecurityAlgorithms.HmacSha256);

            var token = new JwtSecurityToken(
                issuer: _jwtSettings.Issuer,
                audience: _jwtSettings.Audience,
                claims: claims,
                notBefore: DateTime.Now,
                expires: DateTime.Now.AddMinutes(30),
                signingCredentials: cred);

            return Ok(new { token = new JwtSecurityTokenHandler().WriteToken(token) });
        }
    }
}